package com.breakfast.fun.ui.user;

import com.breakfast.fun.Constant;
import com.breakfast.fun.data.entity.user.Result;
import com.breakfast.fun.data.entity.user.User;
import com.breakfast.fun.data.entity.user.coupon.Coupon;
import com.breakfast.fun.util.Api;
import com.breakfast.fun.util.L;
import com.breakfast.fun.util.T;

import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/8/23.
 */
public class UserPresenter extends UserContract.Presenter {


    @Override
    public void getUser() {
        if (Constant.getUserData() != null) {
            Map<String, String> map = Api.getInstance().getBaseParams();
            map.put("action", "info");
            map.put("id", Constant.getUserData().getId() + "");

            rxManager.add(model.getUserModel(map)
                    .subscribeOn(Schedulers.computation())
                    .observeOn(AndroidSchedulers.mainThread())
                    .unsubscribeOn(Schedulers.io())
                    .subscribe(new Subscriber<User>() {
                        @Override
                        public void onCompleted() {
                        }

                        @Override
                        public void onError(Throwable e) {
                            T.show("获取数据失败");
                            view.stopRefresh();
                            e.printStackTrace();
                        }

                        @Override
                        public void onNext(User user) {
                            view.refresh(user);
                            getCode();
                        }

                    }));
        }
    }

    @Override
    public void getCode() {
        Map<String, String> map = Api.getInstance().getBaseParams();
        rxManager.add(model.getRecommendCodeModel(map).subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Result>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Result result) {
                        if (result.isSuccess()) {
                            view.saveCode(result.getData().toString());
                        }
                    }

                }));
    }


    @Override
    public void getCouponCount() {
        Map<String, String> map = Api.getInstance().getBaseParams();

        if (Constant.getUserData() != null) {
            map.put("userId", Constant.getUserData().getId() + "");
            rxManager.add(model.getCouponCountModel(map)
                    .subscribeOn(Schedulers.computation())
                    .observeOn(AndroidSchedulers.mainThread())
                    .unsubscribeOn(Schedulers.io())
                    .subscribe(new Subscriber<Result>() {
                        @Override
                        public void onCompleted() {
                        }

                        @Override
                        public void onError(Throwable e) {

                            T.show("获取数据失败");
                            hideLoadingDialog();
                            view.stopRefresh();
                            e.printStackTrace();
                        }

                        @Override
                        public void onNext(Result result) {
                            hideLoadingDialog();
                            view.stopRefresh();
                            if (result.isSuccess()) {
                                view.setCouponCount((int) ((double) result.getData()));
                            } else {
                                T.show(result.getMsg());
                            }
                        }

                    }));
        }
    }

    @Override
    public void getUnPayCount() {
        Map<String, String> map = Api.getInstance().getBaseParams();

        if (Constant.getUserData() != null) {
            map.put("userId", Constant.getUserData().getId() + "");
            rxManager.add(model.getUnPayCount(map)
                    .subscribeOn(Schedulers.computation())
                    .observeOn(AndroidSchedulers.mainThread())
                    .unsubscribeOn(Schedulers.io())
                    .subscribe(new Subscriber<Result>() {
                        @Override
                        public void onCompleted() {
                        }

                        @Override
                        public void onError(Throwable e) {
                            T.show("获取数据失败");
                            e.printStackTrace();
                        }

                        @Override
                        public void onNext(Result result) {
                            if (result.isSuccess()) {
                                view.setUnPayCount(result.getTotal());
                            } else {
                                T.show(result.getMsg());
                            }
                        }

                    }));
        }
    }


    @Override
    public void onStart() {
        getUser();
        getCouponCount();
        getUnPayCount();
    }
}
